import { createRouter, createWebHistory } from 'vue-router'
import indexView from '@/views/indexView.vue'  //  索引页
import shouyeView from '@/views/wode/shouyeView.vue'  //  首页
import my from '@/views/wode/my.vue'
import userRoutes from './userRoutes.js'
import goodsRoutes from './goodsRoutes.js'
import { showToast } from 'vant'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      component: indexView,

      children: [{
        path: '',
        name: 'home',
        component: shouyeView,
        meta: {
          index: 0,
          cache: true
        }
      }, {
        path: 'cate',
        name: 'cate',
        component: () => import('@/views/wode/fenleiView.vue'),
        meta: {
          index: 1,
          cache: true
        }
      }, {
        path: 'cart',
        name: 'cart',
        component: () => import('@/views/gouwuche/gouwuView.vue'),
        meta: {
          index: 2,
          cache: true
        }
      }, {
        path: 'my',
        name: 'my',
        component: my,
        meta: {
          index: 3,
          cache: true
        }
      }]


    },

    ...userRoutes,
    ...goodsRoutes

  ]
});


// before   什么之前

router.beforeEach((to, from, next) => {

  // to   去的页面
  // from  从哪个页面来的
  // next  函数  

  // console.log(to)
  // console.log(from)
  let uid = localStorage.getItem('uid')

  if (to.meta.auth) {

    //  去的页面需要查验身份
    if (!uid) {

      showToast('请先登陆')
      next({
        path: "/mynotlog"
      })
      return
    } else {
      next();//  去你该去的页面
    }
  } else {
    next()
  }
})



export default router